import React, { Component } from 'react';

export default class ErrorBoundary extends Component {
  state = {
    hasError: false,
  }

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, info) {
    console.error('ErrorBoundary caught an error:', error, info);
    // 在此处可以记录错误
  }

  render() {
    if (this.state.hasError) {
      // 渲染备用 UI
      return <h1>报错了，请稍后再试</h1>;
    }
    return this.props.children;
  }
}
